package club.mzywucai.blog.file_system.pojo.domain;

import club.mzywucai.blog.file_system.mapper.DeleteNotActiveUserMapper;
import club.mzywucai.blog.file_system.pojo.po.NotActiveUserPo;
import club.mzywucai.blog.file_system.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author mzywucai
 * @Description 删除没有激活的用户
 * @date 2019/2/13
 */
@Component
@PropertySource(value = {"classpath:config/application.properties"},encoding = "UTF-8")
public class DelNotActiveUser {
    @Value("${server.mail.host}")
    private String mailHost;
    @Value("${server.mail.account}")
    private String username;
    @Value("${server.mail.password}")
    private String mailPassword;

    @Value("${server.mail.sender}")
    private String personalName;

    @Value("${server.mail.active.fail.head.name}")
    private String mail_head_name;
    @Value("${server.mail.active.fail.head.value}")
    private String mail_head_value;

    @Value("${server.mail.account}")
    private String mail_from;
    @Value("${server.mail.active.fail.subject}")
    private String mail_subject;
    @Value("${server.mail.active.fail.content}")
    private String mail_body;




    @Autowired
    DeleteNotActiveUserMapper deleteNotActiveUserMapper;

    @Scheduled(cron = "0 0 0 * * ?")
    public void delNotActiveUser() {
        // 拿到所有没有激活的用户
        List<NotActiveUserPo> notActiveUserList =
                deleteNotActiveUserMapper.getNotActiveUserList(Judge.NO);

        // List<NotActiveUserPo> notActiveUserListShouldDel = new ArrayList<>();
        List<Long> notActiveUserListShouldDelId = new ArrayList<>();
        List<String> notActiveUserListShouldDelEmail = new ArrayList<>();

        for (NotActiveUserPo po : notActiveUserList) {
            Date registerDate = po.getRegisterDate();
            Date nowDate = new Date();
            int registerDay = DateUtil.getDay(registerDate);
            int nowDay = DateUtil.getDay(nowDate);

            if (nowDay - registerDay >= 2) {
                notActiveUserListShouldDelId.add(po.getUserId());
                notActiveUserListShouldDelEmail.add(po.getEmail());
            }
        }
        // 进行未激活用户删除动作
        deleteNotActiveUserMapper.deleteNotActiveUser(notActiveUserListShouldDelId);

        // 进行删除未激活用户邮箱发送通知
        for (String email : notActiveUserListShouldDelEmail) {
            SendMail sendMail = new SendMail();
            sendMail.setHost(mailHost);
            sendMail.setUsername(username);
            sendMail.setPassword(mailPassword);
            sendMail.setMail_head_name(mail_head_name);
            sendMail.setMail_head_value(mail_head_value);

            sendMail.setMail_from(mail_from);
            sendMail.setMail_subject(mail_subject);
            sendMail.setMail_body(mail_body);
            sendMail.setPersonalName(personalName);

            try {
                sendMail.send(email, null);
            } catch (Exception e) {
                System.err.println("向指定邮箱发送激活链接失败："+email);
            }
        }
    }
}
